
package com.compuvest.webstatus;

// <editor-fold defaultstate="collapsed" desc="import">
import com.compuvest.web.MySQL;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// </editor-fold>

public class WebStatus extends HttpServlet {

    public static final Logger log = LoggerFactory.getLogger(WebStatus.class);

    public MySQL mysql;
    private StatMap<String,Integer> webInfo;

// <editor-fold defaultstate="collapsed" desc="init()">

    @Override
    public void init(ServletConfig config) throws ServletException {
	super.init(config);

        log.info("WebStatus init:");
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="processRequest()">

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");

        String id = request.getParameter("id");
	if(id != null && "0d33c0c15a4838dde68165434188".equals(id)) {
//            webInfo = (StatMap<String,Integer>)request.getSession().getServletContext().getAttribute("webInfo");

            mysql = (MySQL)request.getSession().getServletContext().getAttribute("sb");
            if(mysql == null) {
                log.error("processRequest() new mysql=null");
                return;
            }
            webInfo = mysql.loadWebStatistics();

            try(PrintWriter out = response.getWriter()) {
		out.print(webInfo.getJSON());
            }
        }
/*        else if(id != null && "0d33c0c15a4838dde681654clean".equals(id)) {
            // clean for new date
            StatMap<String,Integer> newInfo = new StatMap<String,Integer>();
            try {
                newInfo.init(StatMap.TODAY_VISIT);
                newInfo.init(StatMap.TODAY_ORDERS);
                newInfo.init(StatMap.TODAY_NEW_CUSTOMER);
                newInfo.init(StatMap.TODAY_OLD_CUSTOMER);
                newInfo.init(StatMap.NOT_FILLED_FORM);
                newInfo.init(StatMap.RESELLER);
                newInfo.init(StatMap.YES_FILLED_FORM);

                newInfo.put(StatMap.ACTIVE, (Integer)webInfo.get(StatMap.ACTIVE));
                newInfo.put(StatMap.TOTAL_VISIT, (Integer)webInfo.get(StatMap.TOTAL_VISIT));
                newInfo.put(StatMap.TOTAL_CUSTOMER, (Integer)webInfo.get(StatMap.TOTAL_CUSTOMER));
                newInfo.put(StatMap.TOTAL_ORDERS, (Integer)webInfo.get(StatMap.TOTAL_ORDERS));

                webInfo.clear();
                webInfo.putAll(newInfo);
            }
            catch(Exception e) {
                Logger.getLogger(WebStatus.class.getName()).log(Level.SEVERE, "clean: ", e.getMessage());
            }
        }*/
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods.">
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }
    @Override
    public String getServletInfo() {
        return "WebStatus";
    }

    @Override
    public void destroy() {
	log.info("Destroy()");
    }
// </editor-fold>

}
